package helper

import (
	"errors"
	"reflect"
	//"strings"
)

//判断一个元素是否在slice、array、map中
func Contains(obj interface{}, target interface{}) (bool, error) {
    targetValue := reflect.ValueOf(target)
    switch reflect.TypeOf(target).Kind() {
        case reflect.Slice, reflect.Array:
            for i := 0; i < targetValue.Len(); i++ {
                if targetValue.Index(i).Interface() == obj {
                   return true, nil
                }
            }
        case reflect.Map:
            if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() {
                return true, nil
            }
    }
    return false, errors.New("not in")
}

func Remove(slice []string, elems ...string) []string {
    isInElems := make(map[string]bool)
    for _, elem := range elems {
        isInElems[elem] = true
    }
    w := 0
    for _, elem := range slice {
        if !isInElems[elem] {
            slice[w] = elem
            w += 1
        }
    }
    return slice[:w]
}

// sliceA在sliceB合法范围之内的元素返回
func SliceInterSection(sliceA []string,sliceB []string)[]string{
	result := []string{}
	for _, i := range sliceA {
		for _, x := range sliceB {
			if i == x {
				result = append(result, i)
			}
		}
	}
	return result
}
/*
func GetSubUpdateFields(reqJson string, validFieldStr string)([]string, error) {
	reqJsonKey, err := GetJsonField(reqJson)
	validFields := strings.Split(validFieldStr,",")
	updateFields := []string{}
	updateFields = SliceInterSection(reqJsonKey, validFields)
	return updateFields, err
}
*/
